package yelmor.comm100.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yelmor.comm100.domain.CoUserCustomMessage;
import yelmor.comm100.domain.IpInfo;
import yelmor.comm100.domain.ServiceRateScore;
import yelmor.comm100.repository.CoUserCustomMessageRepository;
import yelmor.comm100.repository.ServiceRateScoreRepository;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import java.util.Date;

/**
 * Created by yel on 2016/5/6.
 */
@RestController
public class CustomApi {

  @Autowired
  private ServiceRateScoreRepository serviceRateScoreRepository;

  @Autowired
  private CoUserCustomMessageRepository coUserCustomMessageRepository;

  @RequestMapping(path="/api/rateService", method = RequestMethod.POST)
  public void rateService(@RequestParam(value="serviceName") String serviceName,
                          @RequestParam(value="rateScore") Integer rateScore) {
    ServiceRateScore serviceRateScore = new ServiceRateScore();
    serviceRateScore.setRateTime(new Date());
    serviceRateScore.setServiceName(serviceName);
    serviceRateScore.setScore(rateScore);
    serviceRateScoreRepository.save(serviceRateScore);
  }

  @RequestMapping(path="/api/ipaddr", method = RequestMethod.GET)
  public IpInfo getIpInfo(HttpServletRequest httpServletRequest) {
    IpInfo ipInfo = new IpInfo();
    ipInfo.setIpAddress(httpServletRequest.getRemoteAddr());
    return ipInfo;
  }


  @RequestMapping(path="/custom/submitInfo", method = RequestMethod.GET)
  public String customMessageLeave (@RequestParam(value="name") String customName,
                                    @RequestParam(value = "nick") String nickName,
                                    @RequestParam(value = "email") String email,
                                    @RequestParam(value = "phone") String phone,
                                    @RequestParam(value = "message") String message){
    CoUserCustomMessage coUserCustomMessage = new CoUserCustomMessage(
            customName,
            nickName,
            email,
            phone,
            message
    );
    coUserCustomMessageRepository.save(coUserCustomMessage);
    return "ok";
  }
}
